<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use DB;


class Roles extends Authenticatable
{
    use Notifiable;
	protected $table = 'admin_roles';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    // 获取指定角色可见菜单
	public static function getMenuRoles($rid){
		$menus = DB::table('admin_menu')
			->leftjoin('admin_role_menu', 'admin_menu.id', '=', 'admin_role_menu.menu_id')
			->where('admin_role_menu.role_id',$rid)
			->select('admin_menu.id')
			->get()
			->toArray();
		if(!$menus){
			return array();
		}

		foreach ($menus as $menu){
			$menusR[] = $menu->id;
		}
		return $menusR;
	}

	// 根据id获取用户名
	public static function getNameById($id){
		$name = '';
		$name = Roles::where('id',$id)->pluck('role_name');
		if($name){
			$name = $name[0];
		}
		return $name;
	}

	// 角色编辑：获取指定的角色、指定菜单已有的option
	public static function getOneMenuRoleOps($rid,$menu_id){
		$menus = DB::table('admin_role_menu')
			->where('role_id',$rid)
			->where('menu_id',$menu_id)
			->select('option_ids')
			->get()
			->toArray();
		if(!$menus){
			return array();
		}
		foreach ($menus as $menu){
			$menusR = explode(',',$menu->option_ids);
		}
		return $menusR;

	}


}
